const { DataTypes } = require("sequelize");
const { sequelize } = require("../dbConnect");

const User = sequelize.define('users', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    comment: "用户ID"
  },
  username: {
    type: DataTypes.STRING(50),
    allowNull: false,
    unique: true,
    comment: "用户名"
  },
  password: {
    type: DataTypes.STRING(255),
    allowNull: false,
    comment: "密码哈希"
  },
  nickname: {
    type: DataTypes.STRING(100),
    allowNull: true,
    comment: "昵称"
  },
  email: {
    type: DataTypes.STRING(100),
    allowNull: true,
    unique: true,
    comment: "邮箱"
  },
  phone: {
    type: DataTypes.STRING(20),
    allowNull: true,
    comment: "手机号"
  },
  avatar: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: "头像URL"
  },
  gender: {
    type: DataTypes.ENUM('male', 'female', 'other'),
    allowNull: true,
    comment: "性别"
  },
  birth_date: {
    type: DataTypes.DATEONLY,
    allowNull: true,
    comment: "出生日期"
  },
  role: {
    type: DataTypes.ENUM('admin', 'teacher', 'user'),
    defaultValue: 'user',
    comment: "用户角色"
  },
  status: {
    type: DataTypes.ENUM('active', 'inactive', 'banned'),
    defaultValue: 'active',
    comment: "账户状态"
  },
  level: {
    type: DataTypes.INTEGER,
    defaultValue: 1,
    comment: "用户等级"
  },
  experience: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: "经验值"
  },
  total_study_time: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: "总学习时长(秒)"
  },
  practice_count: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: "练习次数"
  },
  points: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: "积分"
  },
  achievements: {
    type: DataTypes.JSON,
    allowNull: true,
    comment: "成就列表"
  },
  last_login_at: {
    type: DataTypes.DATE,
    allowNull: true,
    comment: "最后登录时间"
  }
}, {
  tableName: 'users',
  indexes: [
    {
      unique: true,
      fields: ['username']
    },
    {
      unique: true,
      fields: ['email']
    },
    {
      fields: ['phone']
    }
  ]
});

module.exports = User;
